package com.ews.onlineexchange.rest;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSONObject;
import com.ews.onlineexchange.app.entity.ServiceBuyerStat;
import com.ews.onlineexchange.controller.StatExport;
import com.ews.onlineexchange.service.ServiceBuyerService;
import com.ews.onlineexchange.util.DateUtil;
import com.ews.onlineexchange.util.WorkBookUtil;

@Path( value = "/serviceBuyer" )
public class ServiceBuyerRest {
	@Inject
	EntityManager		entityManager;
	
	private Date		sDate;
	private Date		eDate;

	@Inject
	ServiceBuyerService	serviceBuyerService;

	@GET
	@Path( "/getServiceBuyerStat" )
	@Produces( "application/json" )
	@Context
	public JSONObject getServiceBuyerStat( @QueryParam( "pageSize" ) Integer pageSize,
			@QueryParam( "pageNumber" ) Integer pageNumber, @QueryParam( "startDate" ) String startDate,
			@QueryParam( "endDate" ) String endDate, @QueryParam( "buyerName" ) String buyerName ) {
		if ( pageSize == null )
			pageSize = 5;
		if ( pageNumber == null )
			pageNumber = 1;
		initDateParam( startDate, endDate );
		JSONObject json = serviceBuyerService.stat( pageSize, pageNumber, sDate, eDate, buyerName );

		return json;
	}
	
	@GET
	@Path( "/getServiceBuyerStat/export" )
	@Produces( "application/json" )
	public Response exportServiceBuyerStat( @QueryParam( "pageSize" ) Integer pageSize,
			@QueryParam( "pageNumber" ) Integer pageNumber, @QueryParam( "startDate" ) String startDate,
			@QueryParam( "endDate" ) String endDate, @QueryParam( "buyerName" ) String buyerName ) {
		List<ServiceBuyerStat> suppliers = (List<ServiceBuyerStat>)serviceBuyerService.stat(null, 1, null, null, null).get("list")	;		
		HSSFWorkbook workbook = StatExport.statExportForSupplier("按采购方统计",1,suppliers,false,StatExport.FIELD_LABEL_BUYER,StatExport.FIELD_NAMES_BUYER);		
		
		File tempFile = new File(WorkBookUtil.EXPORT_DATA_DEFAULT_PATH);
		try {
			if(!tempFile.exists()) {
				tempFile.createNewFile();
			}
			workbook.write(tempFile);
			
			ResponseBuilder response = Response.ok( tempFile );
			response.header("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode( "按采购方统计.xls", "UTF-8" ) );
			return response.build();
		} catch ( UnsupportedEncodingException e ) {
			e.printStackTrace();
		} catch ( IOException e ) {
			e.printStackTrace();
		}
		return null;
	}

	public void initDateParam( String startingDate, String endingDate ) {
		if ( org.apache.commons.lang3.StringUtils.isNotBlank( startingDate ) ) {
			try {
				sDate = DateUtil.getDate( startingDate );
			} catch ( Exception e ) {
				// TODO: handle exception
			}
		}
		if ( org.apache.commons.lang3.StringUtils.isNotBlank( endingDate ) ) {
			try {
				Date tempDate = DateUtil.getDate( endingDate );
				if ( tempDate != null ) {
					Calendar cal = Calendar.getInstance();
					cal.setTime( tempDate );
					cal.set( Calendar.HOUR_OF_DAY, 23 );
					cal.set( Calendar.MINUTE, 59 );
					cal.set( Calendar.SECOND, 59 );
					this.eDate = cal.getTime();
				}
				sDate = DateUtil.getDate( startingDate );
			} catch ( Exception e ) {
				// TODO: handle exception
			}
		}
	}

	public Date getsDate() {
		return sDate;
	}

	public void setsDate( Date sDate ) {
		this.sDate = sDate;
	}

	public Date geteDate() {
		return eDate;
	}

	public void seteDate( Date eDate ) {
		this.eDate = eDate;
	}

}
